package com.efast.dmom.mgmt.controller.les.pda;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.dmom.mgmt.bean.les.GroundingBean;
import com.efast.dmom.mgmt.bean.les.PdaTwInbTaskDetailsBean;
import com.efast.dmom.mgmt.entity.les.TwInbTaskDetail;
import com.efast.dmom.mgmt.entity.les.TwInbTaskHeader;
import com.efast.dmom.mgmt.service.les.TwInbTaskDetailService;
import com.efast.dmom.mgmt.service.les.TwInbTaskHeaderService;
import com.efast.dmom.mgmt.utils.Assist;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

/**
 * <p>
 * PDA跟踪号上架：查询数据接口，确认上架的接口
 * </p>
 *
 * @author ChenLiPeng
 */
@Controller
@RequestMapping("/pdaShipping")
@Api(description="pda->入库->PDA跟踪号上架")
public class PdaTwInbTaskDetailController {

	private final TwInbTaskDetailService twInbTaskDetailService;
	private final TwInbTaskHeaderService twInbTaskHeaderService;

	@Autowired
	public PdaTwInbTaskDetailController(TwInbTaskDetailService twInbTaskDetailService,
			TwInbTaskHeaderService twInbTaskHeaderService) {
		this.twInbTaskDetailService = twInbTaskDetailService;
		this.twInbTaskHeaderService = twInbTaskHeaderService;
	}

	/**
	 * 按跟踪号上架是指按照托盘标签条码/箱标签条码进行上架， 根据条码从上架任务表中查询对对应的信息展示在页面上，从而进行上架确认
	 *
	 * @param twInbTaskDetail
	 *            twInbTaskDetail
	 */
	@ApiOperation(value = "【查询数据接口】")
	@RequestMapping(value = "/queryData", method = RequestMethod.POST)
	public @ResponseBody ResultBean queryData(@RequestBody TwInbTaskDetail twInbTaskDetail) {
		ResultBean result = new ResultBean(true, "'");
		Assist assist = new Assist();
		// part_pack_serial 零件包装流水号,
		if (twInbTaskDetail.getPartPackSerial() != null) {
			assist.setRequires(Assist.andEq("part_pack_serial", twInbTaskDetail.getPartPackSerial()));
		}
		// inb_serial_no 入库包装流水号,
		if (twInbTaskDetail.getInbSerialNo() != null) {
			assist.setRequires(Assist.andEq("inb_serial_no", twInbTaskDetail.getInbSerialNo()));
		}
		if (twInbTaskDetail.getPartPackSerial() == null && twInbTaskDetail.getInbSerialNo() == null) {
			throw new ServiceException("查询条件不能为空");
		}
		List<TwInbTaskDetail> twInbTaskDetails = twInbTaskDetailService.selectTwInbTaskDetail(assist);
		result.setObject(twInbTaskDetails);
		return result;
	}

	/**
	 * 按跟踪号上架是指按照托盘标签条码/箱标签条码进行上架， 根据条码从上架任务表中查询对对应的信息展示在页面上，从而进行上架确认
	 *
	 * @param pdaTwInbTaskDetailsBean
	 *            pdaTwInbTaskDetailsBean
	 */
	@ApiOperation(value = "【确认上架】")
	@RequestMapping(value = "/grounding", method = RequestMethod.POST)
	public @ResponseBody ResultBean grounding(@RequestBody PdaTwInbTaskDetailsBean pdaTwInbTaskDetailsBean) {
		ResultBean result = new ResultBean(true, "'");
		if (pdaTwInbTaskDetailsBean.getPdaTwInbTaskDetailBean().size() == 0) {
			throw new ServiceException("待上架数据为空");
		}
		GroundingBean groundingBean = new GroundingBean();
		groundingBean.setTwInbTaskDetail(pdaTwInbTaskDetailsBean.getPdaTwInbTaskDetailBean());
		TwInbTaskHeader twInbTaskHeader = twInbTaskHeaderService.selectTwInbTaskHeaderById(
				pdaTwInbTaskDetailsBean.getPdaTwInbTaskDetailBean().get(0).getTwInbTaskHeaderId());
		if (twInbTaskHeader == null) {
			throw new ServiceException("待上架detail无法查询到对应的header");
		}
		groundingBean.setTwInbTaskHeader(twInbTaskHeader);
		twInbTaskDetailService.grounding(groundingBean);
		return result;
	}

}
